package com.letfox.ad.service.impl;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.letfox.ad.enums.MaterialEnums;
import com.letfox.ad.model.ADChargeUnit;
import com.letfox.ad.model.ADPlan;
import com.letfox.ad.model.ADServingDetail;
import com.letfox.ad.model.ADServingMaterial;
import com.letfox.ad.model.ADServingSchedule;
import com.letfox.ad.model.ADServingSetting;
import com.letfox.ad.model.ADUnitSchedule;
import com.letfox.ad.model.relation.ADPlanMater;
import com.letfox.ad.model.relation.ADPlanSchedule;
import com.letfox.ad.model.relation.ADPlanServingDetail;
import com.letfox.ad.model.relation.ADPlanSetting;
import com.letfox.ad.model.relation.ADPlanUnit;
import com.letfox.ad.service.ADChargeUnitService;
import com.letfox.ad.service.ADPlanService;
import com.letfox.ad.service.ADServingDetailService;
import com.letfox.ad.service.ADServingMaterialService;
import com.letfox.ad.service.ADServingScheduleService;
import com.letfox.ad.service.ADServingSettingService;
import com.letfox.ad.service.ADVertServingService;
import com.letfox.ad.service.relation.ADPlanMaterService;
import com.letfox.ad.service.relation.ADPlanScheduleServcie;
import com.letfox.ad.service.relation.ADPlanServingDetailService;
import com.letfox.ad.service.relation.ADPlanSettingService;
import com.letfox.ad.service.relation.ADPlanUnitService;
import com.letfox.ad.user.model.ADPlanUAccount;
import com.letfox.ad.user.service.ADPlanUAccountService;
import com.letfox.common.enums.AuthEnum;
import com.letfox.common.model.ResultResponse;

@Service
public class ADvertServingServiceImpl implements ADVertServingService {
	
	@Autowired
	private ADPlanService planService;
	@Autowired
	private ADChargeUnitService chargeUnitService;
	@Autowired
	private ADServingScheduleService servingScheduleService;
	@Autowired
	private ADPlanScheduleServcie planRlScheduleServcie;
	@Autowired
	private ADPlanUnitService PlanRlUnitService;
	@Autowired
	private ADServingSettingService servingSettingService;
	@Autowired
	private ADPlanSettingService planRlSettingService;
	@Autowired
	private ADServingMaterialService servingMaterialService;
	@Autowired
	private ADPlanMaterService planRlMaterService;
	@Autowired
	private ADServingDetailService servingDetailService;
	@Autowired
	private ADPlanServingDetailService planRlServingDetailService;
	@Autowired
	private ADPlanUAccountService planRlUAccountService;

	@Override
	@Transactional
	public ResultResponse adFound(ADUnitSchedule adUnitSchedule,String userId) throws Exception {	
		ResultResponse resultResponse=new ResultResponse();
		//AD_PLAN数据添加
		ADPlan adPlan=new ADPlan();
		//唯一标示
		adPlan.setAdPlanId(UUID.randomUUID().toString());
		//广告计划名称
		adPlan.setAdPlanName(adUnitSchedule.getPlanName());
		//每日预算（元)
		adPlan.setAdPlanBudgetDaily(adUnitSchedule.getAdPlanBudgetDaily());
		//计划起始日期
		adPlan.setStartTime(adUnitSchedule.getStartTime());
		//计划结束日期
		adPlan.setEndTime(adUnitSchedule.getEndTime());
		//创建时间
		adPlan.setCreateTime(new Date());
		//修改时间
		adPlan.setUpdateTime(new Date());
		//创建人
		adPlan.setCreateBy("system");
		//修改人
		adPlan.setUpdateBy("system");
		//是否有效数据
		adPlan.setDel(MaterialEnums.Date.YES.getValue());
		//添加数据库
		planService.insert(adPlan);
		//AD_CHARGE_UNIT数据添加
		ADChargeUnit adChargeUnit=new ADChargeUnit();
		//唯一标示
		adChargeUnit.setAdUnitId(UUID.randomUUID().toString());
		//出价类型
		adChargeUnit.setAdUnitPayType(adUnitSchedule.getAdUnitPayType());
		//投放金额（元）
		adChargeUnit.setAdUnitAmount(adUnitSchedule.getAdUnitAmount());
		//竞价出价（元）
		adChargeUnit.setAdUnitQuotePrice(adUnitSchedule.getAdUnitQuotePrice());
		//流量类型
		adChargeUnit.setAdUnitFlowType(adUnitSchedule.getAdUnitFlowType());
		//投放频率
		adChargeUnit.setAdUnitFreq(adUnitSchedule.getAdUnitFreq());
		//创建时间
		adChargeUnit.setCreateTime(new Date());
		//修改时间
		adChargeUnit.setUpdateTime(new Date());
		//创建人
		adChargeUnit.setCreateBy("system");
		//修改人
		adChargeUnit.setUpdateBy("system");
		//是否有效数据
		adChargeUnit.setDel(MaterialEnums.Date.YES.getValue());
		//写入数据
		chargeUnitService.insert(adChargeUnit);
		//AD_SERVING_ SCHEDULE数据添加
		ADServingSchedule adServingSchedule=new ADServingSchedule();
		//唯一标示
		adServingSchedule.setAdScheduleId(UUID.randomUUID().toString());
		//天数
		adServingSchedule.setAdScheduleDay(adUnitSchedule.getAdScheduleDay());
		//小时
		adServingSchedule.setAdScheduleHour(adUnitSchedule.getAdScheduleHour());
		//是否投放
		adServingSchedule.setAdScheduleIsnot(adUnitSchedule.getAdScheduleIsnot());
		//创建时间
		adServingSchedule.setCreateTime(new Date());
		//修改时间
		adServingSchedule.setUpdateTime(new Date());
		//创建人
		adServingSchedule.setCreateBy("system");
		//修改人
		adServingSchedule.setUpdateBy("system");
		//是否有效数据
		adServingSchedule.setDel(MaterialEnums.Date.YES.getValue());
		//写入数据
		servingScheduleService.insert(adServingSchedule);
		
		//AD_PLAN_RL_SCHEDULE添加数据
		ADPlanSchedule adPlanRlSchedule =new ADPlanSchedule();
		//广告计划ID
		adPlanRlSchedule.setAdPlanId(adPlan.getAdPlanId());
		//广告投放时间ID
		adPlanRlSchedule.setAdScheduleId(adServingSchedule.getAdScheduleId());
		//创建时间
		adPlanRlSchedule.setCreateTime(new Date());
		//修改时间
		adPlanRlSchedule.setUpdateTime(new Date());
		//创建人
		adPlanRlSchedule.setCreateBy("system");
		//修改人
		adPlanRlSchedule.setUpdateBy("system");
		//是否有效数据
		adPlanRlSchedule.setDel(MaterialEnums.Date.YES.getValue());
		//写入数据
		planRlScheduleServcie.insert(adPlanRlSchedule);
		
		//AD_PLAN_RL_UNIT添加数据
		ADPlanUnit adPlanRlUnit=new ADPlanUnit();
		//广告计划ID
		adPlanRlUnit.setAdPlanId(adPlan.getAdPlanId());
		//广告计费ID
		adPlanRlUnit.setAdUnitId(adChargeUnit.getAdUnitId());
		//创建时间
		adPlanRlUnit.setCreateTime(new Date());
		//修改时间
		adPlanRlUnit.setUpdateTime(new Date());
		//创建人
		adPlanRlUnit.setCreateBy("system");
		//修改人
		adPlanRlUnit.setUpdateBy("system");
		//是否有效数据
		adPlanRlUnit.setDel(MaterialEnums.Date.YES.getValue());
		//写入数据
		PlanRlUnitService.insert(adPlanRlUnit);	
		
		//关联账号
	    ADPlanUAccount adPlanRlUAccount=new ADPlanUAccount();
	    //账号ID
	    adPlanRlUAccount.setAccountId(userId);
	    //广告设计ID
	    adPlanRlUAccount.setAdPlanId(adPlan.getAdPlanId());
		//创建时间
	    adPlanRlUAccount.setCreateTime(new Date());
		//修改时间
	    adPlanRlUAccount.setUpdateTime(new Date());
		//创建人
	    adPlanRlUAccount.setCreateBy("system");
		//修改人
	    adPlanRlUAccount.setUpdateBy("system");
		//是否有效数据
	    adPlanRlUAccount.setDel(MaterialEnums.Date.YES.getValue());
		//写入数据
		planRlUAccountService.insert(adPlanRlUAccount);
		//返回相应
		resultResponse.setStatusCode(AuthEnum.R200.getCode());
		return resultResponse;
	}

	@Override
	public ResultResponse saveAdServing(ADServingSetting adServingSetting, String planId)
			throws Exception {
		ResultResponse resultResponse=new ResultResponse();
		//唯一标示
		adServingSetting.setAdSetId(UUID.randomUUID().toString());
		//创建时间
		adServingSetting.setCreateTime(new Date());
		//修改时间
		adServingSetting.setUpdateTime(new Date());
		//创建人
		adServingSetting.setCreateBy("system");
		//修改人
		adServingSetting.setUpdateBy("system");
		//是否有效数据
		adServingSetting.setDel(MaterialEnums.Date.YES.getValue());
		//AD_SERVING_SETTING写入数据
		servingSettingService.insert(adServingSetting);
		
		//AD_PLAN_RL_SETTING添加数据
		ADPlanSetting adPlanRlSetting=new ADPlanSetting();
		//广告计划ID
		adPlanRlSetting.setAdPlanId(planId);
		//广告设置ID
		adPlanRlSetting.setAdSetId(adServingSetting.getAdSetId());
		//创建时间
		adPlanRlSetting.setCreateTime(new Date());
		//修改时间
		adPlanRlSetting.setUpdateTime(new Date());
		//创建人
		adPlanRlSetting.setCreateBy("system");
		//修改人
		adPlanRlSetting.setUpdateBy("system");
		//是否有效数据
		adPlanRlSetting.setDel(MaterialEnums.Date.YES.getValue());
		//写入数据
		planRlSettingService.insert(adPlanRlSetting);
		//返回相应
		resultResponse.setStatusCode(AuthEnum.R200.getCode());
		return resultResponse;
	}
	
	public List<ADPlan> selectUserAD(String accountId){
		//查询当前用户创建的广告计划
		String del=MaterialEnums.Date.YES.getValue();
		List<ADPlan> list=planService.selectUserAD(accountId,del);
		return list;
	}

	@Override
	public ResultResponse saveADservingMaterlal(ADServingMaterial adServingMaterial,String planId) {
		ResultResponse resultResponse=new ResultResponse();
		//唯一标示
		adServingMaterial.setAdMaterialId(UUID.randomUUID().toString());
		//创建时间
		adServingMaterial.setCreateTime(new Date());
		//修改时间
		adServingMaterial.setUpdateTime(new Date());
		//创建人
		adServingMaterial.setCreateBy("system");
		//修改人
		adServingMaterial.setUpdateBy("system");
		//是否有效数据
		adServingMaterial.setDel(MaterialEnums.Date.YES.getValue());
		//写入数据
		servingMaterialService.insert(adServingMaterial);
		
		//AD_PLAN_RL_MATER添加数据
		ADPlanMater adPlanRlMater=new ADPlanMater();
		//投放素材ID
		adPlanRlMater.setAdMaterId(adServingMaterial.getAdMaterialId());
		//广告设计ID
		adPlanRlMater.setAdPlanId(planId);
		//创建时间
		adPlanRlMater.setCreateTime(new Date());
		//修改时间
		adPlanRlMater.setUpdateTime(new Date());
		//创建人
		adPlanRlMater.setCreateBy("system");
		//修改人
		adPlanRlMater.setUpdateBy("system");
		//是否有效数据
		adPlanRlMater.setDel(MaterialEnums.Date.YES.getValue());
		//写入数据
		planRlMaterService.insert(adPlanRlMater);
		//返回相应
		resultResponse.setStatusCode(AuthEnum.R200.getCode());
		return resultResponse;
	}

	@Override
	public ResultResponse saveAdDetal(ADServingDetail adServingDetail,String planId) throws Exception {
		ResultResponse resultResponse=new ResultResponse();
		//唯一标示
		adServingDetail.setAdServingId(UUID.randomUUID().toString());
		//创建时间
		adServingDetail.setCreateTime(new Date());
		//修改时间
		adServingDetail.setUpdateTime(new Date());
		//创建人
		adServingDetail.setCreateBy("system");
		//修改人
		adServingDetail.setUpdateBy("system");
		//是否有效数据
		adServingDetail.setDel(MaterialEnums.Date.YES.getValue());
		//写入数据AD_SERVING_DETAIL 
		servingDetailService.insert(adServingDetail);
		
		//AD_PLAN_RL_SERVING_DETAIL 数据添加
		ADPlanServingDetail adPlanRlServingDetail=new ADPlanServingDetail();
		//广告计划ID
		adPlanRlServingDetail.setAdPlanId(planId);
		//投放明细ID
		adPlanRlServingDetail.setAdServingId(adServingDetail.getAdServingId());
		//创建时间
		adPlanRlServingDetail.setCreateTime(new Date());
		//修改时间
		adPlanRlServingDetail.setUpdateTime(new Date());
		//创建人
		adPlanRlServingDetail.setCreateBy("system");
		//修改人
		adPlanRlServingDetail.setUpdateBy("system");
		//是否有效数据
		adPlanRlServingDetail.setDel(MaterialEnums.Date.YES.getValue());
		//写入数据
		planRlServingDetailService.insert(adPlanRlServingDetail);
		//返回相应
		resultResponse.setStatusCode(AuthEnum.R200.getCode());
		return resultResponse;
	}
}